package com.chenhy.controller;

import com.chenhy.pojo.Bill;
import com.chenhy.pojo.Provider;
import com.chenhy.service.BillService;
import com.chenhy.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private ProviderService providerService;

    @GetMapping("/billList")
    public String queryAllBill(Model model){
        List<Bill> bills = billService.queryAllBill();
        model.addAttribute("billList", bills);
        return "bill/billList";
    }

    @GetMapping("/fuzzyQueryBill")
    public String fuzzyBillQuery(String condition, Model model){
        List<Bill> bills = billService.fuzzyBillQuery(condition);
        model.addAttribute("billList", bills);
        return "bill/billList.html::billSpace";
    }

    @GetMapping("/addBill")
    public String toAddBill(Model model){
        List<Provider> providers = providerService.queryAllProvider();
        model.addAttribute("providerList", providers);
        return "bill/addBill";
    }

    @PostMapping("/addBill")
    public String addBill(Bill bill, Model model){
        billService.addBill(bill);
        model.addAttribute("msg", "订单添加成功，可继续添加");
        return "bill/addBill";
    }

    @GetMapping("/billDetails")
    public String toBillDetails(int id, Model model){
        Bill bill = billService.queryBillById(id);
        model.addAttribute("bill",bill);
        return "bill/billDetails";
    }

    @GetMapping("/updateBill")
    public String toUpdateBill(int id, Model model){
        Bill bill = billService.queryBillById(id);
        List<Provider> providers = providerService.queryAllProvider();
        model.addAttribute("providerList", providers);
        model.addAttribute("bill", bill);
        return "bill/updateBill";
    }

    @PostMapping("/updateBill")
    public String updateBill(Bill bill){
        billService.updateBill(bill);
        return "bill/updateBill";
    }


    @Secured({"ROLE_ADMIN","ROLE_MANAGER"})
    @PostMapping("/deleteBill")
    public String deleteBill(int id){
        billService.deleteBill(id);
        return "redirect:billList";
    }


}
